﻿using UnityEngine;
using System.Collections;
using CommonFactory;
using MessageSystem;

namespace CharacterSystem
{
    /// <summary>
    /// 角色孵化器基类
    /// </summary>
    public class CharacterSpawnBase : MonoBehaviour, ICharacterSpawn
    {
        CommonFactory<CharacterBase,HeroData> factory;

        MessageTerminal messageTerminal;

        public void SetFactory(CommonFactory<CharacterBase, HeroData> factory)
        {
            this.factory = factory;
        }

        void Awake()
        {
            CharacterSystemFacade.Inst.RegisterSpawn(this);
            messageTerminal = this.gameObject.AddComponent<MessageTerminal>();
            messageTerminal.RegisterMessage("StartSpawn", OnMessage);
        }

        public void OnMessage(IMessageArgs args)
        {
            CreateItem();
        }

        void OnDestroy()
        {
            CharacterSystemFacade.Inst.RemoveSpawn(this);
        }

        public CharacterBase CreateItem()
        {
            CharacterBase character=factory.CreateProduct();
            character.transform.position = this.transform.position;
            OnCreated(character);
            return character;
        }

        public virtual void OnCreated(CharacterBase character)
        {

        }
    }
}